package com.procurement.inquiry.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.procurement.inquiry.domain.PcmApplicationDetail;
import com.procurement.inquiry.domain.PcmInquiryDetail;

import com.procurement.inquiry.domain.PcmSupplier;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RenotePcmService;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.procurement.inquiry.domain.PcmInquiryDemand;
import com.procurement.inquiry.service.IPcmInquiryDemandService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 询报价需求Controller
 * 
 * @author ruoyi
 * @date 2023-11-21
 */
@RestController
@RequestMapping("/demand")
public class PcmInquiryDemandController extends BaseController
{
    @Autowired
    private IPcmInquiryDemandService pcmInquiryDemandService;
    @Autowired(required = false)
    private RenotePcmService renotePcmService;

    /**
     * 查询询报价需求列表
     */
    @RequiresPermissions("inquiry:demand:list")
    @GetMapping("/list")
    public TableDataInfo list(PcmInquiryDemand pcmInquiryDemand)
    {
        startPage();
        List<PcmInquiryDemand> list = pcmInquiryDemandService.selectPcmInquiryDemandList(pcmInquiryDemand);
//        list.forEach(e->{
//            System.out.println(e.getpc);
//        });
        list.forEach(e->{
            e.getPcmApplicationDetailList().forEach(f->{
//                System.out.println(f.getExpectedSupplier());
                PcmSupplier pcmSupplier=pcmInquiryDemandService.selectSupplier(f.getExpectedSupplier());
                if (pcmSupplier==null){
                    PcmSupplier pcmSupplier1=new PcmSupplier();
                    f.setPcmSupplier(pcmSupplier1);
                }else {
                    f.setPcmSupplier(pcmSupplier);
                }

            });
        });
        return getDataTable(list);
    }
    @RequiresPermissions("inquiry:demand:lisstdetail")
    @GetMapping("/lisstdetail")
    public TableDataInfo lisstdetail(PcmApplicationDetail pcmApplicationDetail){
        startPage();
        List<PcmApplicationDetail> list=pcmInquiryDemandService.selectPcmApplicatioDetailList(pcmApplicationDetail);
        list.forEach(e->{

            PcmSupplier pcmSupplier = pcmInquiryDemandService.selectSupplier(e.getExpectedSupplier());
            if (pcmSupplier==null){
                PcmSupplier pcmSupplier1=new PcmSupplier();
                e.setPcmSupplier(pcmSupplier1);
            }else {
                e.setPcmSupplier(pcmSupplier);
            }

        });
        return getDataTable(list);
    }
    /**
     * 导出询报价需求列表
     */
    @RequiresPermissions("inquiry:demand:export")
    @Log(title = "询报价需求", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PcmInquiryDemand pcmInquiryDemand)
    {
        List<PcmInquiryDemand> list = pcmInquiryDemandService.selectPcmInquiryDemandList(pcmInquiryDemand);
        ExcelUtil<PcmInquiryDemand> util = new ExcelUtil<PcmInquiryDemand>(PcmInquiryDemand.class);
        util.exportExcel(response, list, "询报价需求数据");
    }

    /**
     * 获取询报价需求详细信息
     */
    @RequiresPermissions("inquiry:demand:query")
    @GetMapping(value = "/{inquiryDemandNumber}")
    public AjaxResult getInfo(@PathVariable("inquiryDemandNumber") String inquiryDemandNumber)
    {
        return success(pcmInquiryDemandService.selectPcmInquiryDemandByInquiryDemandNumber(inquiryDemandNumber));
    }

    /**
     * 新增询报价需求
     */
    @RequiresPermissions("inquiry:demand:add")
    @Log(title = "询报价需求", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PcmInquiryDemand pcmInquiryDemand)
    {
        pcmInquiryDemand.setSubmitDate(new Date());
        String number=renotePcmService.scbm("XJ", SecurityConstants.INNER);
        Long userId= SecurityUtils.getUserId();
        pcmInquiryDemand.setInquiryDemandNumber(number);
        pcmInquiryDemand.setUserId(userId);
        int a=pcmInquiryDemandService.insertPcmInquiryDemand(pcmInquiryDemand);
        List<PcmApplicationDetail> list=pcmInquiryDemand.getPcmApplicationDetailList();
        list.forEach(e->{
            int v=Integer.valueOf(String.valueOf(e.getDetailId()));
            PcmInquiryDetail pcmInquiryDetail=new PcmInquiryDetail();
            pcmInquiryDetail.setInquiryDemandNumber(pcmInquiryDemand.getInquiryDemandNumber());
            pcmInquiryDetail.setDetail_id(v);
            pcmInquiryDemandService.insertPcmInquiryDetail(pcmInquiryDetail);
        });

        return toAjax(a);
    }

    /**
     * 修改询报价需求
     */
    @RequiresPermissions("inquiry:demand:edit")
    @Log(title = "询报价需求", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PcmInquiryDemand pcmInquiryDemand)
    {
        return toAjax(pcmInquiryDemandService.updatePcmInquiryDemand(pcmInquiryDemand));
    }

    /**
     * 删除询报价需求
     */
    @RequiresPermissions("inquiry:demand:remove")
    @Log(title = "询报价需求", businessType = BusinessType.DELETE)
	@DeleteMapping("/{inquiryDemandNumbers}")
    public AjaxResult remove(@PathVariable String[] inquiryDemandNumbers)
    {
        return toAjax(pcmInquiryDemandService.deletePcmInquiryDemandByInquiryDemandNumbers(inquiryDemandNumbers));
    }
}
